define(['request', 'template','cookie','jquery', 'bootstrap'], (request, template,cookie) => {
  class Header {
    constructor () {
      this.loadHTML().then(() => {
        this.getTabs()
        this.calcCartCount()
        this.getCookie()
        this.delCookie()
      })
    }
    // 先加载页面
    loadHTML () {
      return new Promise(resolve => {
        $('header').load('/html/modules/header.html', () => {
          resolve()
        })
      })
    }
    getTabs () {
      // 利用熊猫优选接口获取分类
      request.getTabs().then(resp => {
        if (resp.code === 200) {
          const { list } = resp.data
          $('#navList').html(template('navList_template', { tabs: list.slice(1,8)}))
        }
      })
    }
    calcCartCount () {
      let count = 0
      let cart = localStorage.getItem('cart')
      if (cart) {
        cart = JSON.parse(cart)
        count = cart.reduce((num, shop) => {
          return num + shop.count
        }, 0)
      }
      $('#shopnum').html(count)
      $('#cart1 span').html(count)
    }
    getCookie() {
      if($.cookie('user')) {
        $('.ydl').show()
        $('.mdl').hide()
      }
    }
    delCookie() {
      $('#tcdl').on('click', function() {
        $.cookie('user','',{expires: -1, path: '/' })
      })
    }
  }
  return new Header()
})
